package com.fjp.heartguard.ui.report

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fjp.heartguard.R
import com.fjp.heartguard.app.Constants
import com.fjp.heartguard.app.base.BaseFragment
import com.fjp.heartguard.app.data.bean.OlderInfo
import com.fjp.heartguard.app.data.bean.ReportsItem
import com.fjp.heartguard.app.eventViewModel
import com.fjp.heartguard.app.ext.init
import com.fjp.heartguard.databinding.FragmentReportMineBinding
import com.fjp.heartguard.ui.report.adapter.OlderAdapter
import com.fjp.heartguard.ui.report.adapter.ReportAdapter
import com.fjp.heartguard.viewmodel.ReportMineViewModel
import me.hgj.jetpackmvvm.ext.parseState

class ReportMineFragment : BaseFragment<ReportMineViewModel, FragmentReportMineBinding>(), TextWatcher {
    val data = arrayListOf<OlderInfo>()
    var hasMore = false
    val reportAdapter = OlderAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        mDatabind.click = ClickProxy()
        mDatabind.searchEt.addTextChangedListener(this)
        mViewModel.getOlderList()
        mDatabind.recyclerView.init(LinearLayoutManager(context, RecyclerView.VERTICAL, false), reportAdapter)
        mDatabind.refreshLayout.setOnRefreshListener {
            println("==========执行了一次refreshLayout")
            mViewModel.page = 1
            mViewModel.getOlderList()
        }
        mDatabind.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                // 获取LinearLayoutManager
                val layoutManager = recyclerView.layoutManager as LinearLayoutManager?

                // 获取最后一个完全显示的item的position
                val lastVisibleItemPosition = layoutManager!!.findLastCompletelyVisibleItemPosition()
                val totalItemCount = layoutManager!!.itemCount

                // 判断是否滑动到底部
                if (lastVisibleItemPosition + 1 >= totalItemCount && hasMore) {
                    // 执行滑动到底部的操作，例如加载更多数据
                    println("==========执行了一次判断是否滑动到底部")
                    mViewModel.getOlderList()
                }
            }
        });
    }

    override fun createObserver() {
        super.createObserver()
        println("==========createObserver")
        eventViewModel.saveQuestionSuccess.observeInFragment(this) {
            mViewModel.page = 1
            mViewModel.getOlderList()
        }

        eventViewModel.updateLocalReport.observeInFragment(this) {
            mViewModel.page = 1
            mViewModel.getOlderList()
        }
        mViewModel.olders.observeForever {
            parseState(it, {
                mDatabind.refreshLayout.isRefreshing = false
                if (mViewModel.page == 1) {
                    data.clear()
                }
                hasMore = it.data.size == mViewModel.pageSize
                println("==========执行了一次")
                eventViewModel.mineReportsAccount.postValue(it.total)
//                eventViewModel.allReportsAccount.postValue(it.all_quest)
                data.addAll(it.data)
                reportAdapter.setList(data)
                reportAdapter.notifyDataSetChanged()
                if (reportAdapter.data.size == 0) {
                    mDatabind.emptyLl.visibility = View.VISIBLE
                } else {
                    mDatabind.emptyLl.visibility = View.GONE
                }
                mViewModel.page++
            }, {
                it.printStackTrace()
                mDatabind.refreshLayout.isRefreshing = false
            })
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        eventViewModel.saveQuestionSuccess.removeObservers(this)
    }
    inner class ClickProxy {
        fun clickSearchIcon() {
            if (mDatabind.searchEt.text.length > 0) {
                mDatabind.searchEt.setText("")
                mDatabind.searchIv.setImageResource(R.drawable.search_icon)
            }
        }

        fun clickRefresh() {
            mViewModel.page = 1
            mViewModel.getOlderList()
        }

    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    override fun afterTextChanged(p0: Editable?) {
        mViewModel.page = 1
        mViewModel.getOlderList()
        if (mDatabind.searchEt.text.isNotEmpty()) {
            mDatabind.searchIv.setImageResource(R.drawable.search_deleted)
        } else {
            mDatabind.searchIv.setImageResource(R.drawable.search_icon)
        }
    }

}